package com.wang.system.manager;

import com.wang.common.core.constants.CacheConstants;
import com.wang.common.redis.service.RedisService;
import com.wang.system.domain.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class UserCacheManager {

    @Autowired
    private RedisService redisService;

    public void updateStatus(Long userId,Integer status){

        String userKey = getUserKey(userId);

        //从缓存中查询当前用户的信息
        User user = redisService.getCacheObject(userKey, User.class);
        if (user == null){
            return;
        }

        //更新用户状态
        user.setStatus(status);
        redisService.setCacheObject(userKey,user);

        //设置用户缓存有效期为10分钟,这里注意不要误解为只拉黑10分钟,因为数据库中的数据还没有更新
        redisService.expire(userKey, CacheConstants.USER_EXP, TimeUnit.MINUTES);
    }

    private String getUserKey(Long userId) {
        //获取userKey-->"user:detail:" + userId
        return CacheConstants.USER_DETAIL + userId;
    }
}
